﻿using JV.Com.Business;
using JV.Com.Business.Log;
using JV.Com.Internals;
using JV.Com.Tools;
using JV.Com.ViewModels.Wrapper;
using JV.Com.Views;
using Microsoft.Practices.Prism.PubSubEvents;
using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.ApplicationModel.Resources;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// Pour plus d'informations sur le modèle Application vide, consultez la page http://go.microsoft.com/fwlink/?LinkId=234227

namespace JV.Com
{
    /// <summary>
    /// Fournit un comportement spécifique à l'application afin de compléter la classe Application par défaut.
    /// </summary>
    sealed partial class App : MvvmAppBase
    {
        private readonly IUnityContainer m_container = new UnityContainer();
        public IEventAggregator EventAggregator { get; set; }

        /// <summary>
        /// Initialise l'objet d'application de singleton.  Il s'agit de la première ligne du code créé
        /// à être exécutée. Elle correspond donc à l'équivalent logique de main() ou WinMain().
        /// </summary>
        public App()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Appelé lorsque la navigation vers une page donnée échoue
        /// </summary>
        /// <param name="sender">Frame à l'origine de l'échec de navigation.</param>
        /// <param name="e">Détails relatifs à l'échec de navigation</param>
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        protected override void OnRegisterKnownTypesForSerialization()
        {
            //SessionStateService.RegisterKnownType(typeof(FlipViewNavigationParameter));
        }

        protected override void OnInitialize(IActivatedEventArgs args)
        {
            EventAggregator = new EventAggregator();

            m_container.RegisterInstance<INavigationService>(NavigationService);
            m_container.RegisterInstance<ISessionStateService>(SessionStateService);
            m_container.RegisterInstance<IEventAggregator>(EventAggregator);

            ViewModelLocator.Register(typeof(MainPage).ToString(), () => new ViewModels.MainPageViewModel(NavigationService));
            ViewModelLocator.Register(typeof(SearchResultPage).ToString(), () => new ViewModels.SearchResultPageViewModel(NavigationService));
            ViewModelLocator.Register(typeof(NewsListPage).ToString(), () => new ViewModels.NewsListPageViewModel(NavigationService));
            ViewModelLocator.Register(typeof(DetailGamePage).ToString(), () => new ViewModels.DetailGamePageViewModel(NavigationService));
            ViewModelLocator.Register(typeof(FolderPage).ToString(), () => new ViewModels.FolderPageViewModel(NavigationService));
            ViewModelLocator.Register(typeof(TestGamePage).ToString(), () => new ViewModels.TestGamePageViewModel(NavigationService));
            ViewModelLocator.Register(typeof(PreviewGamePage).ToString(), () => new ViewModels.PreviewGamePageViewModel(NavigationService));
            ViewModelLocator.Register(typeof(FlipViewFullScreenPage).ToString(), () => new ViewModels.FlipViewFullScreenPageViewModel(NavigationService));
        }

        protected async override Task OnLaunchApplication(LaunchActivatedEventArgs args)
        {
            await Cache.InitAsync();
            DispatcherHelper.Init(Windows.ApplicationModel.Core.CoreApplication.GetCurrentView().CoreWindow.Dispatcher);

            // Needed for transition
            ((Frame)Window.Current.Content).Background = Application.Current.Resources["ApplicationBackgroundThemeBrush"] as LinearGradientBrush;

            EventListener listener = new StorageFileEventListener("ListenerError");
            listener.EnableEvents(MetroEventSource.Log, EventLevel.LogAlways);

            NavigationService.Navigate("Main", null);
        }
    }
}
